/*
 * Copyright (C) 2008,2009,2010,2011  Paulo Cesar Pereira de Andrade.
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * Authors:
 *	Paulo Cesar Pereira de Andrade
 */

#include "exl.h"

#define r0			thread_self->r0
#define ss			thread_self->ess
#define bp			thread_self->ebp
#define sp			thread_self->esp
#define obj			thread_self->obj
#define ret			thread_self->ret
#define flt			thread_self->flt

static void
cqq_canonicalize(void);

/*
 * Implementation
 */
void
evm_qq_ne(evalue_t *v)
{
    r0.t = t_int;
    switch (v->t) {
	case t_cdd:
	    r0.v.i = real(v->v.cdd) != mpq_get_d(cqq_realref(r0.v.cqq)) ||
		     imag(v->v.cdd) != mpq_get_d(cqq_imagref(r0.v.cqq));
	    break;
	case t_cqq:		case t_sqq:
	    r0.v.i = mpq_cmp(cqq_realref(v->v.cqq), cqq_realref(r0.v.cqq)) ||
		     mpq_cmp(cqq_imagref(v->v.cqq), cqq_imagref(r0.v.cqq));
	    break;
	case t_mpc:		case t_shc:
	    mpfr_set_q(thr_rr0, cqq_realref(r0.v.cqq), thr_rnd);
	    if (mpfr_equal_p(mpc_realref(v->v.mpc), thr_rr0)) {
		mpfr_set_q(thr_rr0, cqq_imagref(r0.v.cqq), thr_rnd);
		r0.v.i = !mpfr_equal_p(mpc_imagref(v->v.mpc), thr_rr0);
	    }
	    else
		r0.v.i = 1;
	    break;
	default:
	    r0.v.i = 1;
	    break;
    }
}

void
evm_qq_eq(evalue_t *v)
{
    r0.t = t_int;
    switch (v->t) {
	case t_cdd:
	    r0.v.i = real(v->v.cdd) == mpq_get_d(cqq_realref(r0.v.cqq)) &&
		     imag(v->v.cdd) == mpq_get_d(cqq_imagref(r0.v.cqq));
	    break;
	case t_cqq:		case t_sqq:
	    r0.v.i = !mpq_cmp(cqq_realref(v->v.cqq), cqq_realref(r0.v.cqq)) &&
		     !mpq_cmp(cqq_imagref(v->v.cqq), cqq_imagref(r0.v.cqq));
	    break;
	case t_mpc:		case t_shc:
	    mpfr_set_q(thr_rr0, cqq_realref(r0.v.cqq), thr_rnd);
	    if (mpfr_equal_p(mpc_realref(v->v.mpc), thr_rr0)) {
		mpfr_set_q(thr_rr0, cqq_imagref(r0.v.cqq), thr_rnd);
		r0.v.i = mpfr_equal_p(mpc_imagref(v->v.mpc), thr_rr0);
	    }
	    else
		r0.v.i = 0;
	    break;
	default:
	    r0.v.i = 0;
	    break;
    }
}

static void
cqq_canonicalize(void)
{
    if (!mpq_sgn(thr_qi)) {
	if (mpz_cmp_ui(mpq_denref(thr_qr), 1))
	    set_rq();
	else if (empz_fit_i(thr_zr)) {
	    r0.t = t_int;
	    r0.v.i = empz_get_i(thr_zr);
	}
	else
	    set_rz();
    }
    else
	chk_rqq();
}

void
evm_qq_add(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    break;
	case t_int:
	    eqq_add_i_qq(thr_qq, v->v.i, r0.v.cqq);
	    chk_rqq();
	    break;
	case t_float:
	    r0.t = t_cdd;
	    r0.v.cdd = edd_add_d_qq(v->v.d, r0.v.cqq);
	    chk_rdd();
	    break;
	case t_mpz:		case t_shz:
	    eqq_add_z_qq(thr_qq, v->v.mpz, r0.v.cqq);
	    chk_rqq();
	    break;
	case t_mpq:		case t_shq:
	    eqq_add_q_qq(thr_qq, v->v.mpq, r0.v.cqq);
	    chk_rqq();
	    break;
	case t_mpr:		case t_shr:
	    ecc_add_r_qq(thr_c, v->v.mpr, r0.v.cqq);
	    set_chk_rrr();
	    break;
	case t_cdd:
	    r0.t = t_cdd;
	    r0.v.cdd = edd_add_dd_qq(v->v.cdd, r0.v.cqq);
	    chk_rdd();
	    break;
	case t_cqq:		case t_sqq:
	    eqq_add_qq_qq(thr_qq, v->v.cqq, r0.v.cqq);
	    cqq_canonicalize();
	    break;
	case t_mpc:		case t_shc:
	    ecc_add_cc_qq(thr_c, v->v.mpc, r0.v.cqq);
	    set_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_add_cqq(v->v.o, r0.v.cqq, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_qq_sub(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    cqq_neg(thr_qq, r0.v.cqq);
	    chk_rqq();
	    break;
	case t_int:
	    eqq_sub_i_qq(thr_qq, v->v.i, r0.v.cqq);
	    chk_rqq();
	    break;
	case t_float:
	    r0.t = t_cdd;
	    r0.v.cdd = edd_sub_d_qq(v->v.d, r0.v.cqq);
	    chk_rdd();
	    break;
	case t_mpz:		case t_shz:
	    eqq_sub_z_qq(thr_qq, v->v.mpz, r0.v.cqq);
	    chk_rqq();
	    break;
	case t_mpq:		case t_shq:
	    eqq_sub_q_qq(thr_qq, v->v.mpq, r0.v.cqq);
	    chk_rqq();
	    break;
	case t_mpr:		case t_shr:
	    ecc_sub_r_qq(thr_c, v->v.mpr, r0.v.cqq);
	    set_chk_rrr();
	    break;
	case t_cdd:
	    r0.t = t_cdd;
	    r0.v.cdd = edd_sub_dd_qq(v->v.cdd, r0.v.cqq);
	    chk_rdd();
	    break;
	case t_cqq:		case t_sqq:
	    eqq_sub_qq_qq(thr_qq, v->v.cqq, r0.v.cqq);
	    cqq_canonicalize();
	    break;
	case t_mpc:		case t_shc:
	    ecc_sub_cc_qq(thr_c, v->v.mpc, r0.v.cqq);
	    set_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_sub_cqq(v->v.o, r0.v.cqq, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_qq_mul(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    r0.t = t_int;
	    r0.v.i = 0;
	    break;
	case t_int:
	    if (likely(v->v.i != 0)) {
		eqq_mul_i_qq(thr_qq, v->v.i, r0.v.cqq);
		chk_rqq();
	    }
	    else {
		r0.t = t_int;
		r0.v.i = 0;
	    }
	    break;
	case t_float:
	    r0.t = t_cdd;
	    r0.v.cdd = edd_mul_d_qq(v->v.d, r0.v.cqq);
	    chk_rdd();
	    break;
	case t_mpz:		case t_shz:
	    eqq_mul_z_qq(thr_qq, v->v.mpz, r0.v.cqq);
	    chk_rqq();
	    break;
	case t_mpq:		case t_shq:
	    eqq_mul_q_qq(thr_qq, v->v.mpq, r0.v.cqq);
	    chk_rqq();
	    break;
	case t_mpr:		case t_shr:
	    ecc_mul_r_qq(thr_c, v->v.mpr, r0.v.cqq);
	    set_chk_rrr();
	    break;
	case t_cdd:
	    r0.t = t_cdd;
	    r0.v.cdd = edd_mul_dd_qq(v->v.cdd, r0.v.cqq);
	    chk_rdd();
	    break;
	case t_cqq:		case t_sqq:
	    eqq_mul_qq_qq(thr_qq, v->v.cqq, r0.v.cqq);
	    cqq_canonicalize();
	    break;
	case t_mpc:		case t_shc:
	    ecc_mul_cc_qq(thr_c, v->v.mpc, r0.v.cqq);
	    set_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_mul_cqq(v->v.o, r0.v.cqq, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_qq_div(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    r0.t = t_int;
	    r0.v.i = 0;
	    break;
	case t_int:
	    if (likely(v->v.i != 0)) {
		eqq_div_i_qq(thr_qq, v->v.i, r0.v.cqq);
		chk_rqq();
	    }
	    else {
		r0.t = t_int;
		r0.v.i = 0;
	    }
	    break;
	case t_float:
	    r0.t = t_cdd;
	    r0.v.cdd = edd_div_d_qq(v->v.d, r0.v.cqq);
	    chk_rdd();
	    break;
	case t_mpz:		case t_shz:
	    eqq_div_z_qq(thr_qq, v->v.mpz, r0.v.cqq);
	    chk_rqq();
	    break;
	case t_mpq:		case t_shq:
	    eqq_div_q_qq(thr_qq, v->v.mpq, r0.v.cqq);
	    chk_rqq();
	    break;
	case t_mpr:		case t_shr:
	    ecc_div_r_qq(thr_c, v->v.mpr, r0.v.cqq);
	    set_chk_rrr();
	    break;
	case t_cdd:
	    r0.t = t_cdd;
	    r0.v.cdd = edd_div_dd_qq(v->v.cdd, r0.v.cqq);
	    chk_rdd();
	    break;
	case t_cqq:		case t_sqq:
	    eqq_div_qq_qq(thr_qq, v->v.cqq, r0.v.cqq);
	    cqq_canonicalize();
	    break;
	case t_mpc:		case t_shc:
	    ecc_div_cc_qq(thr_c, v->v.mpc, r0.v.cqq);
	    set_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_div_cqq(v->v.o, r0.v.cqq, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_qq_atan2(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    if (!flt) {
		r0.t = t_cdd;
		r0.v.cdd = edd_atan2_d_qq(0.0, r0.v.cqq);
		chk_rdd();
	    }
	    else {
		ecc_atan2_i_qq(thr_c, 0, r0.v.cqq);
		set_chk_rrr();
	    }
	    break;
	case t_int:
	    if (!flt) {
		r0.t = t_cdd;
		r0.v.cdd = edd_atan2_i_qq(v->v.i, r0.v.cqq);
		chk_rdd();
	    }
	    else {
		ecc_atan2_i_qq(thr_c, v->v.i, r0.v.cqq);
		set_chk_rrr();
	    }
	    break;
	case t_float:
	    r0.t = t_cdd;
	    r0.v.cdd = edd_atan2_d_qq(v->v.d, r0.v.cqq);
	    chk_rdd();
	    break;
	case t_mpz:		case t_shz:
	    if (!flt) {
		r0.t = t_cdd;
		r0.v.cdd = edd_atan2_z_qq(v->v.mpz, r0.v.cqq);
		chk_rdd();
	    }
	    else {
		ecc_atan2_z_qq(thr_c, v->v.mpz, r0.v.cqq);
		set_chk_rrr();
	    }
	    break;
	case t_mpq:		case t_shq:
	    if (!flt) {
		r0.t = t_cdd;
		r0.v.cdd = edd_atan2_q_qq(v->v.mpq, r0.v.cqq);
		chk_rdd();
	    }
	    else {
		ecc_atan2_q_qq(thr_c, v->v.mpq, r0.v.cqq);
		set_chk_rrr();
	    }
	    break;
	case t_mpr:		case t_shr:
	    ecc_atan2_r_qq(thr_c, v->v.mpr, r0.v.cqq);
	    set_chk_rrr();
	    break;
	case t_cdd:
	    r0.t = t_cdd;
	    r0.v.cdd = edd_atan2_dd_qq(v->v.cdd, r0.v.cqq);
	    chk_rdd();
	    break;
	case t_cqq:		case t_sqq:
	    if (!flt) {
		r0.t = t_cdd;
		r0.v.cdd = edd_atan2_qq_qq(v->v.cqq, r0.v.cqq);
		chk_rdd();
	    }
	    else {
		ecc_atan2_qq_qq(thr_c, v->v.cqq, r0.v.cqq);
		set_chk_rrr();
	    }
	    break;
	case t_mpc:		case t_shc:
	    ecc_atan2_cc_qq(thr_c, v->v.mpc, r0.v.cqq);
	    set_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_atan2_cqq(v->v.o, r0.v.cqq, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_qq_pow(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    if (!flt) {
		r0.t = t_cdd;
		r0.v.cdd = edd_pow_d_qq(0.0, r0.v.cqq);
		chk_rdd();
	    }
	    else {
		ecc_pow_i_qq(thr_c, 0, r0.v.cqq);
		set_chk_rrr();
	    }
	    break;
	case t_int:
	    if (!flt) {
		r0.t = t_cdd;
		r0.v.cdd = edd_pow_i_qq(v->v.i, r0.v.cqq);
		chk_rdd();
	    }
	    else {
		ecc_pow_i_qq(thr_c, v->v.i, r0.v.cqq);
		set_chk_rrr();
	    }
	    break;
	case t_float:
	    r0.t = t_cdd;
	    r0.v.cdd = edd_pow_d_qq(v->v.d, r0.v.cqq);
	    chk_rdd();
	    break;
	case t_mpz:		case t_shz:
	    if (!flt) {
		r0.t = t_cdd;
		r0.v.cdd = edd_pow_z_qq(v->v.mpz, r0.v.cqq);
		chk_rdd();
	    }
	    else {
		ecc_pow_z_qq(thr_c, v->v.mpz, r0.v.cqq);
		set_chk_rrr();
	    }
	    break;
	case t_mpq:		case t_shq:
	    if (!flt) {
		r0.t = t_cdd;
		r0.v.cdd = edd_pow_q_qq(v->v.mpq, r0.v.cqq);
		chk_rdd();
	    }
	    else {
		ecc_pow_q_qq(thr_c, v->v.mpq, r0.v.cqq);
		set_chk_rrr();
	    }
	    break;
	case t_mpr:		case t_shr:
	    ecc_pow_r_qq(thr_c, v->v.mpr, r0.v.cqq);
	    set_chk_rrr();
	    break;
	case t_cdd:
	    r0.t = t_cdd;
	    r0.v.cdd = edd_pow_dd_qq(v->v.cdd, r0.v.cqq);
	    chk_rdd();
	    break;
	case t_cqq:		case t_sqq:
	    if (!flt) {
		r0.t = t_cdd;
		r0.v.cdd = edd_pow_qq_qq(v->v.cqq, r0.v.cqq);
		chk_rdd();
	    }
	    else {
		ecc_pow_qq_qq(thr_c, v->v.cqq, r0.v.cqq);
		set_chk_rrr();
	    }
	    break;
	case t_mpc:		case t_shc:
	    ecc_pow_cc_qq(thr_c, v->v.mpc, r0.v.cqq);
	    set_chk_rrr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_pow_cqq(v->v.o, r0.v.cqq, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}

void
evm_qq_hypot(evalue_t *v)
{
    switch (v->t) {
	case t_void:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_abs_qq(r0.v.cqq);
	    }
	    else {
		er_abs_qq(thr_rr, r0.v.cqq);
		set_rr();
	    }
	    break;
	case t_int:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_hypot_i_qq(v->v.i, r0.v.cqq);
	    }
	    else {
		er_hypot_i_qq(thr_rr, v->v.i, r0.v.cqq);
		set_rr();
	    }
	    break;
	case t_float:
	    r0.t = t_float;
	    r0.v.d = ed_hypot_d_qq(v->v.d, r0.v.cqq);
	    break;
	case t_mpz:		case t_shz:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_hypot_z_qq(v->v.mpz, r0.v.cqq);
		break;
	    }
	    else {
		er_hypot_z_qq(thr_rr, v->v.mpz, r0.v.cqq);
		set_rr();
	    }
	    break;
	case t_mpq:		case t_shq:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_hypot_q_qq(v->v.mpq, r0.v.cqq);
	    }
	    else {
		er_hypot_q_qq(thr_rr, v->v.mpq, r0.v.cqq);
		set_rr();
	    }
	    break;
	case t_mpr:		case t_shr:
	    er_hypot_r_qq(thr_rr, v->v.mpr, r0.v.cqq);
	    set_rr();
	    break;
	case t_cdd:
	    r0.t = t_float;
	    r0.v.d = ed_hypot_dd_qq(v->v.cdd, r0.v.cqq);
	    break;
	case t_cqq:		case t_sqq:
	    if (!flt) {
		r0.t = t_float;
		r0.v.d = ed_hypot_qq_qq(v->v.cqq, r0.v.cqq);
	    }
	    else {
		er_hypot_qq_qq(thr_rr, v->v.cqq, r0.v.cqq);
		set_rr();
	    }
	    break;
	case t_mpc:		case t_shc:
	    er_hypot_cc_qq(thr_rr, v->v.mpc, r0.v.cqq);
	    set_rr();
	    break;
	default:
	    if (!evector_type_p(v->t))
		evm_raise(except_not_a_number);
	    evector_hypot_cqq(v->v.o, r0.v.cqq, v->t & ~(t_const|t_vector));
	    r0.v.o = obj;
	    r0.t = eobject_type(obj);
	    break;
    }
}
